`
#!/bin/bash
PUBLISHER="No Starch Press"
print_name(){
local name
name="Black Hat Bash"
echo "${name} by ${PUBLISHER}"
}
print_name
echo "The variable ${name} will not be printed because it is a local variable."
We assign the value No Starch Press to the variable
publisher, then create a function called print_name(). (
You’ll learn more about functions in the next chapter.) Within the
function, we declare a local variable called name and assign it the
value "Black Hat Bash". Then we call the print_name()
function and attempt to access the name variable as part of a
sentence to be printed using echo.
The echo command at the end of the script file will result in an
empty variable, as the name variable is locally scoped to the
print_name() function, which means that nothing outside of the
function can access it. So, it will simply return without a value.
Save this script, remembering to set the executable permission
using chmod, and run it using the following command:
$ ./local_scope_variable.sh
Black Hat Bash by No Starch Press
The variable will not be printed because it is a local variable.
This script is available at https://github.com/dolevf/Black-Hat-
Bash/blob/master/ch01/local_scope_variable.sh.
Arithmetic Operators
Arithmetic operators allow you to perform mathematical
operations on integers. Table 1-3 shows some of the arithmetic
operators available. The full list of the available arithmetic operators
can be found at https://tldp.org/LDP/abs/html/ops.html.
Table 1-3
Arithmetic Operators
Operator
Description
Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks